package cn.icanci.loopstack.ddk.web.controller.config;

import cn.icanci.loopstack.ddk.admin.biz.service.GroupService;
import cn.icanci.loopstack.ddk.common.model.PageList;
import cn.icanci.loopstack.ddk.common.model.TextValue;
import cn.icanci.loopstack.ddk.common.model.config.GroupVO;
import cn.icanci.loopstack.ddk.common.result.R;
import cn.icanci.loopstack.ddk.web.form.GroupQueryForm;
import cn.icanci.loopstack.ddk.web.mapping.GroupWebMapping;
import cn.icanci.loopstack.ddk.web.model.Group;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

/**
 * @author icanci
 * @since 1.0 Created in 2023/01/07 18:17
 */
@RestController
@RequestMapping("/ddk/group")
public class GroupController {
    @Resource
    private GroupService    groupService;

    @Resource
    private GroupWebMapping groupWebMapping;

    @PostMapping("query")
    public R query(@RequestBody GroupQueryForm form) {
        PageList<GroupVO> queryPage = groupService.queryPage(groupWebMapping.web2vo(form.getGroup()), form.getPaginator().getCurrentPage(), form.getPaginator().getPageSize());
        PageList<Group> ret = new PageList<>(groupWebMapping.vos2webs(queryPage.getData()), queryPage.getPaginator());
        return R.builderOk().data("queryPage", ret).build();
    }

    @PostMapping("save")
    public R save(@RequestBody Group group) {
        groupService.save(groupWebMapping.web2vo(group));
        return R.builderOk().build();
    }

    @GetMapping("validateGroupId/{groupId:.*}")
    public R validateGroupId(@PathVariable("groupId") String groupId) {
        GroupVO group = groupService.queryByGroupId(groupId);
        return R.builderOk().data("result", group == null).build();
    }

    @GetMapping("validateGroupName/{groupName:.*}")
    public R validateGroupName(@PathVariable("groupName") String groupName) {
        GroupVO group = groupService.queryByGroupName(groupName);
        return R.builderOk().data("result", group == null).build();
    }

    @GetMapping("loadSelector")
    public R loadSelector() {
        List<TextValue> textValues = groupService.loadSelector();
        return R.builderOk().data("textValues", textValues).build();
    }

}
